package com.hjw.controller;

import com.hjw.entity.Skill;
import com.hjw.service.ISkillService;
import com.hjw.util.PageResult;
import com.hjw.util.R;
import com.hjw.vo.SkillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hjw
 * @since 2025-01-12
 */
@RestController
@RequestMapping("/skill")
public class SkillController {
    @Autowired
    private ISkillService skillService;
    @RequestMapping("/list")
    public R findAll(){
        return R.ok().data("skills",skillService.list());
    }
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize){
        PageResult<Skill> skills = skillService.findByPage(page,pageSize);
        System.out.println(skills);
        return R.ok().data("skills",skills);
    }
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) SkillVo vo){
        PageResult<Skill> skills = skillService.search(page,pageSize,vo);
        return R.ok().data("skills",skills);
    }
    @PostMapping("/save")
    public R saveSkill(@RequestBody Skill skill){
        return skillService.save(skill) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }
    @PutMapping("/update")
    public R updateSkill(@RequestBody Skill skill){
        return skillService.updateById(skill) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }
    @DeleteMapping("/delete/{ids}")
    public R deleteSkill(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        System.out.println(sids);
        return skillService.removeByIds(sids) ? R.ok().message("删除技能成功!") : R.error().message("删除产品失败!");
    }
}
